<template>
  <div style="width: 100%;">

    <div style="width: 100%;text-align: right">
      <el-button type="primary" @click="handleCreate" >我要反馈</el-button>
    </div>
    <div class="user-activity" v-for="item in list ">

      <div class="post">

        <div class="user-block">
          <img class="img-circle" :src="avatar">
          <span class="username text-muted">{{name}}</span>
          <span class="description">发布于:{{item.add_time}}</span>

        </div>
        <p>
          {{item.title}}
        </p>
        <ul class="list-inline" style="text-align: right">
          <li @click="handleDelete([item.id])">
          <span class="link-black text-sm" style="cursor: pointer">
            <i class="el-icon-delete" />
            删除
          </span>
          </li>
          <li @click="handleUpdate(item)">
          <span class="link-black text-sm" style="cursor: pointer">
            <i class="el-icon-edit" />
            修改
          </span>
          </li>
        </ul>
      </div>


    </div>
    <el-dialog :close-on-press-escape="false" :close-on-click-modal="false" :title="textMap[dialogStatus]" :visible="dialogFormVisible" width="800px" :before-close="()=>dialogFormVisible=false">
      <div>
        <el-form ref="dataForm" :model="temp" :rules="rules" label-width="120px" style="padding-right:32px;min-width: 700px">
          <el-row :gutter="2">
            <el-col :span="24">
              <el-form-item label="标题" prop="title">
                <el-input v-model="temp.title" />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="反馈内容" prop="content">
                <tinymce v-model="temp.content" :height="300" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          {{ $t('table.cancel') }}
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          {{ $t('table.confirm') }}
        </el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
import {mapGetters} from "vuex";
import Tinymce from "@/components/Tinymce/index.vue";
import {createData, delData, fetchList, updateData} from "@/api/common";
import _ from "lodash";

export default {
  components: {Tinymce},
  data() {
    return {
      dialogFormVisible: false,
      dialogStatus: 'create',
      textMap: {
       update: this.$t("bs.edit"),
        create: this.$t("bs.add")
      },
      temp:{
        id: '', title: "", add_user_id: '', content: '', add_time:""
      },
      dialogPvVisible: false,
      carouselImages: [
        'https://wpimg.wallstcn.com/9679ffb0-9e0b-4451-9916-e21992218054.jpg',
        'https://wpimg.wallstcn.com/bcce3734-0837-4b9f-9261-351ef384f75a.jpg',
        'https://wpimg.wallstcn.com/d1d7b033-d75e-4cd6-ae39-fcd5f1c0a7c5.jpg',
        'https://wpimg.wallstcn.com/50530061-851b-4ca5-9dc5-2fead928a939.jpg'
      ],

      rules: {
        title: [{ required: true, message: '标题不能为空', trigger: 'blur' }],
        content: [{ required: true, message: '内容不能为空', trigger: 'blur' }],
      },
      listQuery: {
        page_no: 1,
        limit: 1000,
        sort: 'id desc',
        where: {}
      },
      list:[],
      total:0,
      listLoading:false,
    }
  },
  created() {
    Date.prototype.Format = function(formatStr)  {
      var str = formatStr;
      var Week = ['日','一','二','三','四','五','六'];

      str=str.replace(/yyyy|YYYY/,this.getFullYear());
      str=str.replace(/yy|YY/,(this.getYear() % 100)>9?(this.getYear() % 100).toString():'0' + (this.getYear() % 100));

      str=str.replace(/MM/,this.getMonth()>9?this.getMonth().toString():'0' + (this.getMonth()+1));
      str=str.replace(/M/g,this.getMonth());


      str=str.replace(/w|W/g,Week[this.getDay()]);

      str=str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():'0' + this.getDate());
      str=str.replace(/d|D/g,this.getDate());

      str=str.replace(/hh|HH/,this.getHours()>9?this.getHours().toString():'0' + this.getHours());
      str=str.replace(/h|H/g,this.getHours());
      str=str.replace(/mm/,this.getMinutes()>9?this.getMinutes().toString():'0' + this.getMinutes());
      str=str.replace(/m/g,this.getMinutes());

      str=str.replace(/ss|SS/,this.getSeconds()>9?this.getSeconds().toString():'0' + this.getSeconds());
      str=str.replace(/s|S/g,this.getSeconds());

      return str;
    }
    this.getList()
  },
  computed: {
    ...mapGetters([
      'name',
      'avatar',
      'roles'
    ])
  },
  methods:{

    getList() {
      this.listLoading = true
      this.list=[];
      this.listQuery.where['and']=[{ field: 'add_user_id', operator: '=', value: this.$store.state.user.user_id }]
      fetchList(this.listQuery, 'sl_comment').then(response => {
        if (response.data && response.data.items) {
          this.list = response.data.items
          this.total = response.data.count
        }
        this.listLoading = false
      })
    },
    resetTemp() {
      this.temp = {
        id: '', title: "", content: ""
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        if(this.$refs['dataForm']){
          this.$refs['dataForm'].clearValidate()
        }
      })
    },
    createData()  {
      console.log('数据',this.$store.state)
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const temp = _.cloneDeep(this.temp)
          temp.add_user_id=this.$store.state.user.user_id;
          var date=new Date();
          temp.add_time=date.Format("yyyy-MM-dd HH:mm:ss");
          const data = {
            modelname: 'comment',
            add: temp
          }

          createData({ data: [data] }, 'sl_comment').then(() => {
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        if(this.$refs['dataForm']){
          this.$refs['dataForm'].clearValidate()
        }

      })
    },
    updateData() {
      console.log('数据',this.$store.state)
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const temp = _.cloneDeep(this.temp)
          temp.add_user_id=this.$store.state.user.user_id;
          var date=new Date();
          temp.add_time=date.Format("yyyy-MM-dd HH:mm:ss");
          const data = {
            modelname: 'comment',
            update: temp
          }
          updateData({ data: [data] }, 'sl_comment').then(() => {
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        }
      })
    },
    handleDeleteAll() {
      console.log('删除全部', this.multipleSelection)
      if (!this.multipleSelection || this.multipleSelection.length == 0) {
        this.$message.error('请选择需要删除的数据')
        return
      }
      this.$confirm('你确定删除这' + this.multipleSelection.length + '条数据么?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const ids = []
        for (var i = 0; i < this.multipleSelection.length; i++) {
          ids.push(this.multipleSelection[i].id)
        }
        this.handleDelete(ids)
      })
    },
    handleDelete(ids) {
      console.log('删除', ids)
      delData({ id: ids.join(',') }, 'sl_comment').then(() => {
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 2000
        })
        this.getList()
      })
    },

  }
}
</script>

<style lang="scss" scoped>
.user-activity {
  .user-block {

    .username,
    .description {
      display: block;
      margin-left: 50px;
      padding: 2px 0;
    }

    .username{
      font-size: 16px;
      color: #000;
    }

    :after {
      clear: both;
    }

    .img-circle {
      border-radius: 50%;
      width: 40px;
      height: 40px;
      float: left;
    }

    span {
      font-weight: 500;
      font-size: 12px;
    }
  }

  .post {
    font-size: 14px;
    border-bottom: 1px solid #d2d6de;
    margin-bottom: 15px;
    padding-bottom: 15px;
    color: #666;

    .image {
      width: 100%;
      height: 100%;

    }

    .user-images {
      padding-top: 20px;
    }
  }

  .list-inline {
    padding-left: 0;
    margin-left: -5px;
    list-style: none;

    li {
      display: inline-block;
      padding-right: 5px;
      padding-left: 5px;
      font-size: 13px;
    }

    .link-black {

      &:hover,
      &:focus {
        color: #999;
      }
    }
  }

}

.box-center {
  margin: 0 auto;
  display: table;
}

.text-muted {
  color: #777;
}
</style>
